home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Sprocket Framework DR2 / Sprocket Framework / SpokenCommandHandler.cp < prev    next >
Text File  |  1996-05-31  |  7KB  |  300 lines

  1. /*
  2.  
  3.     File:        SpokenCommandHandler.cp
  4.     Project:    Sprocket Framework 1.1 b1 of 2/26/96
  5.     Contains:    Class which implements serial number-based protection (ie only X copies allowed
  6.                 to being running at any one time)
  7.     To Do:        ?
  8.  
  9.     Sprocket Major Contributors:
  10.     ----------------------------
  11.     Dave Falkenburg, producer of Sprocket 1.0
  12.     Bill Hayden,     producer of Sprocket 1.1
  13.     Steve Sisak,     producer of the upcoming Sprocket 2.0
  14.     
  15.     Pete Alexander        Dave Mark
  16.     Eric Berdahl        Gary Powell
  17.     Marshall Clow        Leonard Rosenthal
  18.     Tim Craycroft        Steve Sisak
  19.     David denBoer        Jon Summers
  20.     Cameron Esfahani    Randy Thelen
  21.     Steve Falkenburg    Dean Yu
  22.     Nitin Ganatra
  23.     Dave Hershey        Apple Computer, Inc.
  24.     
  25.     Comments, Additions, or Corrections:
  26.     ------------------------------------
  27.     Bill Hayden, Nikol Software <nikol@codewell.com>
  28.  
  29. */
  30.  
  31.  
  32. #include "SpokenCommandHandler.h"
  33. #include "Sprocket.h"
  34. #include <Gestalt.h>
  35.  
  36. #include "UString.h"
  37. #include <AppleEvents.h>
  38.  
  39. #include "AEHandling.h"
  40.  
  41.  
  42. pascal OSErr HandleSpeechDoneAppleEvent (AppleEvent *theAEevt, AppleEvent* reply, long refcon);
  43.  
  44.  
  45.  
  46. /*****************************************************************************/
  47.  
  48.  
  49.  
  50. TSpokenCommandHandler::TSpokenCommandHandler()
  51. {
  52.     fListening = false;
  53. }
  54.  
  55.  
  56.  
  57. /*****************************************************************************/
  58.  
  59.  
  60.  
  61.  
  62. OSErr    TSpokenCommandHandler::ISpokenCommandHandler()
  63. {
  64.     OSErr    status;
  65.     long    attributes;
  66.     
  67.     // Make sure SpeechRecognition Toolbox is available
  68.     
  69.     status = Gestalt (gestaltSpeechRecognitionVersion, &attributes);
  70.     
  71.     // Must be at least version 1.5 to support Speech Recognition Toolbox API
  72.     
  73.     if (!status)
  74.         if (attributes < 0x00000150)
  75.             status = -1;
  76.  
  77.     // Open a Recognition System
  78.     
  79.     if (!status)
  80.         status = SROpenRecognitionSystem (&fRecognitionSystem, kSRDefaultRecognitionSystemID);
  81.  
  82.     // We don't want the default feedback or listening modes
  83.     if (false && !status)
  84.         {
  85.         short feedbackModes = kSRNoFeedbackNoListenModes;
  86.         status = SRSetProperty (fRecognitionSystem, kSRFeedbackAndListeningModes, 
  87.                     &feedbackModes, sizeof (feedbackModes));
  88.         }
  89.         
  90.     // Create a recognizer with default speech source -- e.g. the desktop microphone
  91.     
  92.     if (!status)
  93.         status = SRNewRecognizer (fRecognitionSystem, &fRecognizer, kSRDefaultSpeechSource);
  94.                 
  95.     // Install an AppleEvent handler so recognizer can send us recognition results.
  96.  
  97.     if (!status)
  98.         status = AEInstallEventHandler(kAESpeechSuite, kAESpeechDone,
  99.                 NewAEEventHandlerProc (HandleSpeechDoneAppleEvent), 0, false);
  100.             
  101.     // Make one language model, then make it active
  102.  
  103.     // Make a simple language model (LM)
  104.     
  105.     if (!status)
  106.         status = SRNewLanguageModel (fRecognitionSystem, &fLanguageModel, "SR", clen ("SR"));
  107.     
  108.     // Use this LM in recognition
  109.     
  110.     if (!status)
  111.         status = SRSetLanguageModel (fRecognizer, fLanguageModel);
  112.         
  113.     // To start listening, call StartListening()
  114.             
  115.     return status;
  116. }
  117.  
  118.  
  119.  
  120. /*****************************************************************************/
  121.  
  122.  
  123.  
  124.  
  125. OSErr TSpokenCommandHandler::RegisterSpokenCommand( const char* commandText, CommandID command )
  126. {
  127.     OSErr status;
  128.     
  129.     status = SRAddText (fLanguageModel, commandText, clen(commandText), command);
  130.     
  131.     if (!status)
  132.         status = SRSetLanguageModel (fRecognizer, fLanguageModel);
  133.         
  134.     return status;
  135. }
  136.  
  137.  
  138.  
  139. /*****************************************************************************/
  140.  
  141.  
  142.  
  143. TSpokenCommandHandler::~TSpokenCommandHandler()
  144. {
  145.     OSErr status;
  146.  
  147.     status = SRReleaseObject (fLanguageModel);
  148.  
  149.     status = SRStopListening (fRecognizer);                    // stop processing incoming sound
  150.     status = SRReleaseObject (fRecognizer);                    // balance SRNewRecognizer call
  151.     status = SRCloseRecognitionSystem (fRecognitionSystem);    // balance SROpenRecognitionSystem call
  152.     
  153.     if (status)
  154.         DebugNum(status);
  155. }
  156.  
  157.  
  158.  
  159. /*****************************************************************************/
  160.  
  161.  
  162.  
  163. OSErr    TSpokenCommandHandler::StartListening()
  164. {
  165.     OSErr    err;
  166.     
  167.     
  168.     // start processing incoming sound
  169.     
  170.     err = SRStartListening(fRecognizer);
  171.     
  172.     if (!err)
  173.         fListening = true;
  174.         
  175.     return err;
  176. }
  177.  
  178.  
  179.  
  180. /*****************************************************************************/
  181.  
  182.  
  183.  
  184. OSErr    TSpokenCommandHandler::StopListening()
  185. {
  186.     OSErr    err;
  187.     
  188.     
  189.     // stop processing incoming sound
  190.     
  191.     err = SRStopListening(fRecognizer);
  192.     
  193.     if (!err)
  194.         fListening = false;
  195.         
  196.     return err;
  197. }
  198.  
  199.  
  200.  
  201. /*****************************************************************************/
  202.  
  203.  
  204.  
  205. Boolean    TSpokenCommandHandler::IsListening()
  206. {
  207.     return fListening;
  208. }
  209.  
  210.  
  211.  
  212. /*****************************************************************************/
  213.  
  214. /*  Here's an AppleEvent handler for handling the kAESpeechDone event of the
  215.     kAESpeechSuite -- i.e. the event indicating a recognition attempt was made.
  216.     The keySpeechStatus parameter gives the status of the recognition.  If it
  217.     is noErr, then the keySpeechResult parameter gives a RecognitionResult with the
  218.     various representations of the words the user spoke.
  219. */
  220.  
  221. pascal OSErr HandleSpeechDoneAppleEvent (AppleEvent *theAEevt, AppleEvent* /* reply */, long /* refcon */)
  222. {
  223.     long                actualSize;
  224.     DescType            actualType;
  225.     OSErr                status = 0, recStatus = 0;
  226.     SRRecognitionResult    recResult;
  227.     Str255                str;
  228.     Size                len;
  229.  
  230.  
  231.     // Get status
  232.     
  233.     status = AEGetParamPtr(theAEevt, keySRSpeechStatus, typeInteger,
  234.                     &actualType, (Ptr)&recStatus, sizeof(status), &actualSize);
  235.  
  236.     // Get result
  237.     
  238.     if (!status && !recStatus)
  239.         status = AEGetParamPtr(theAEevt, keySRSpeechResult, typeSRSpeechResult, &actualType,
  240.                                 (Ptr)&recResult, sizeof(SRRecognitionResult), &actualSize);
  241.                     
  242.     // Get text of words in result.
  243.     // Could also get actual phrase, path, or LM objects using other
  244.     // format property selectors. If we did that, we'd also want to
  245.     // release those objects when we were done using them here.
  246.     
  247.     if (!status)
  248.         {
  249.         len = 255L;
  250.         status = SRGetProperty (recResult, kSRTEXTFormat, str+1, &len);
  251.         if (!status)
  252.             {
  253.             str[0] = len;
  254.                 
  255.             // Release RecognitionResult since we are done with it!!! 
  256.             //    The toolbox has given us a reference to this object, and
  257.             //    assumes we are still using it until we call SRReleaseObject
  258.             //    with it.
  259.             }
  260.         }
  261.         
  262.     if (!status)
  263.         {
  264.         SRLanguageModel        LM;
  265.         SRLanguageObject    anObject;
  266.         
  267.         len = sizeof(SRLanguageModel);
  268.         status = SRGetProperty (recResult, kSRLanguageModelFormat, &LM, &len);
  269.         if (!status)
  270.             {
  271.             long    myCommand;
  272.             status = SRGetIndexedItem(LM, &anObject, 0);
  273.                 
  274.             if (!status)
  275.                 {
  276.                 len = sizeof(long);
  277.                 status = SRGetProperty (anObject, kSRRefCon, &myCommand, &len);
  278.                 
  279.                 // Release RecognitionResult since we are done with it!!! 
  280.                 //    The toolbox has given us a reference to this object, and
  281.                 //    assumes we are still using it until we call SRReleaseObject
  282.                 //    with it.
  283.                     
  284.                 if (myCommand != 0)
  285.                     status = SendCommandToSelf(myCommand);
  286.                 
  287.                 SRReleaseObject (anObject);
  288.                 }
  289.                 
  290.             SRReleaseObject (recResult);
  291.             SRReleaseObject (LM);
  292.             }
  293.         }
  294.  
  295.     if (status)
  296.         DebugMessage ("\pSpokenCommandHandler: Recognition error in AE Handler");
  297.  
  298.     return status;
  299. }
  300.